package com.youku.utils
{
   import flash.events.EventDispatcher;
   import flash.utils.*;
   import flash.events.Event;
   import flash.net.SharedObjectFlushStatus;
   import flash.events.NetStatusEvent;
   import com.youku.PlayerConfig;
   import flash.net.SharedObject;
   import flash.net.ObjectEncoding;
   
   public class FSOManager extends EventDispatcher
   {
      
      public function FSOManager(param1:String, param2:String = null, param3:int = -1) {
         var key:Object = null;
         var localID:String = param1;
         var localPath:String = param2;
         var flushInterval:int = param3;
         super();
         this._intervalID = -1;
         this._flushPending = false;
         this._shouldFlush = true;
         SharedObject.defaultObjectEncoding = ObjectEncoding.AMF0;
         try
         {
            this._settings = SharedObject.getLocal(localID,localPath);
            this._settings.objectEncoding = ObjectEncoding.AMF0;
            this._settings.addEventListener(NetStatusEvent.NET_STATUS,this.onNetStatus);
         }
         catch(e:Error)
         {
            _settings = new Object();
            _settings.data = {};
         }
         this._keys = new Array();
         if(this._settings.data == null || this._settings.size == 0)
         {
            this._firstUse = true;
         }
         for(key in this._settings.data)
         {
            this._keys.push(key);
         }
         this.startFlushInterval(flushInterval);
      }
      
      public static const EVENT_ERROR:String = "FSOManager.event_error";
      
      public static const EVENT_FLUSHED:String = "FSOManager.event_flushed";
      
      public static const EVENT_FLUSHING:String = "FSOManager.event_flushing";
      
      private var _firstUse:Boolean;
      
      private var _flushInterval:int;
      
      private var _flushPending:Boolean;
      
      private var _intervalID:int;
      
      private var _keys:Array;
      
      private var _settings;
      
      private var _shouldFlush:Boolean;
      
      public function get isFirstUse() : Boolean {
         return this._firstUse;
      }
      
      public function get settingsKeys() : Array {
         return this._keys;
      }
      
      public function clear(param1:Object) : void {
         var _loc2_:int = this._keys.indexOf(param1);
         if(_loc2_ != -1)
         {
            delete this._settings.data[param1];
            this._keys.splice(_loc2_,1);
         }
      }
      
      public function clearAll() : void {
         var _loc1_:Object = null;
         for each(_loc1_ in this._keys)
         {
            delete this._settings.data[_loc1_];
         }
         this._keys.splice(0,this._keys.length);
      }
      
      public function flush() : void {
         if(!this._shouldFlush)
         {
            return;
         }
         if(this._flushPending)
         {
            return;
         }
         dispatchEvent(new Event(EVENT_FLUSHING));
         try
         {
            if(this._settings.flush() == SharedObjectFlushStatus.PENDING)
            {
               this._flushPending = true;
            }
         }
         catch(e:Error)
         {
         }
      }
      
      public function load(param1:Object, param2:* = null) : Object {
         if(this._settings.data == null)
         {
            return null;
         }
         var _loc3_:String = param2 == null?param1.toString():getQualifiedClassName(param2) + param1.toString();
         return this._settings.data[_loc3_];
      }
      
      private function onNetStatus(param1:NetStatusEvent) : void {
         this._flushPending = false;
         if(param1.info.level == "error")
         {
            this._shouldFlush = false;
            dispatchEvent(new Event(EVENT_ERROR));
         }
         else
         {
            dispatchEvent(new Event(EVENT_FLUSHED));
         }
      }
      
      public function save(param1:Object, param2:Object, param3:* = null, param4:Boolean = false) : void {
         if(PlayerConfig.isTudouPlayer)
         {
            return;
         }
         var _loc5_:String = param3 == null?param1.toString():getQualifiedClassName(param3) + param1.toString();
         this._settings.data[_loc5_] = param2;
         if(this._keys.indexOf(_loc5_) == -1)
         {
            this._keys.push(_loc5_);
         }
         if((param4) || this._flushInterval == -1)
         {
            this.flush();
         }
      }
      
      public function startFlushInterval(param1:Number) : void {
         this._flushInterval = param1;
         if(this._flushInterval > 0)
         {
            this._intervalID = setInterval(this.flush,this._flushInterval);
         }
      }
      
      public function stopFlushInterval() : void {
         if(this._intervalID != -1)
         {
            clearInterval(this._intervalID);
         }
         this._intervalID = -1;
      }
   }
}
